/**
 * @copyright 2015 Indie Semiconductor.
 *
 * This file is proprietary to Indie Semiconductor.
 * All rights reserved. Reproduction or distribution, in whole
 * or in part, is forbidden except by express written permission
 * of Indie Semiconductor.
 *
 * @file adc_device.h
 */

#ifndef __ADC_DEVICE_H__
#define __ADC_DEVICE_H__

#include <stddef.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include "rugby.h"
#include "adc_sfr.h"

typedef enum {
  ADC_MEASURE_ITEM_VLED0 = 0,
  ADC_MEASURE_ITEM_VLED1,
  ADC_MEASURE_ITEM_VLED2,
  ADC_MEASURE_ITEM_VLED3,
  ADC_MEASURE_ITEM_VLED4,
  ADC_MEASURE_ITEM_VLED5,
  ADC_MEASURE_ITEM_VLED6,
  ADC_MEASURE_ITEM_VLED7,
  ADC_MEASURE_ITEM_VLED8,
  ADC_MEASURE_ITEM_VLED9,
  ADC_MEASURE_ITEM_VLED10,
  ADC_MEASURE_ITEM_VLED11,
  ADC_MEASURE_ITEM_VLED12,
  ADC_MEASURE_ITEM_VLED13,
  ADC_MEASURE_ITEM_VLED14,
  ADC_MEASURE_ITEM_VLED15,
  ADC_MEASURE_ITEM_VLED16,
  ADC_MEASURE_ITEM_VLED17,
  ADC_MEASURE_ITEM_VLED18,
  ADC_MEASURE_ITEM_VLED19,
  ADC_MEASURE_ITEM_VLED20,
  ADC_MEASURE_ITEM_VLED21,
  ADC_MEASURE_ITEM_VLED22,
  ADC_MEASURE_ITEM_VLED23,
  ADC_MEASURE_ITEM_VBUCK,
  ADC_MEASURE_ITEM_VBAT,
  ADC_MEASURE_ITEM_VTEMP,
  ADC_MEASURE_ITEM_BANDGAP,
  ADC_MEASURE_ITEM_GPIO1,
  ADC_MEASURE_ITEM_AOE,
  ADC_MEASURE_ITEM_RESVED,
  ADC_MEASURE_ITEM_NONE,
}AdcMeasureItem_t;

typedef enum {
  ADC_SEQ_1_CHANNEL = 0,
  ADC_SEQ_2_CHANNEL,
  ADC_SEQ_3_CHANNEL
}AdcSEQ_t;

typedef enum {
  ADC_SAMPLING_CYCLE_1 = 0,
  ADC_SAMPLING_CYCLE_2,
  ADC_SAMPLING_CYCLE_4,
  ADC_SAMPLING_CYCLE_8,
  ADC_SAMPLING_CYCLE_16,
  ADC_SAMPLING_CYCLE_32,
  ADC_SAMPLING_CYCLE_48,
  ADC_SAMPLING_CYCLE_64,
}AdcSamplingCycle_t;

typedef enum {
  ADC_MODE_DIFF = 0,
  ADC_MODE_SINGLE_END,
}AdcMode_t;

#define CONF_MODE_POS                   (0)
#define CONF_AUTOEN_POS                 (1)
#define CONF_ATTEN_POS                  (2)
#define CONF_SAMPLECYC_POS              (3)
#define CONF_SWSYNCIN_POS               (6)
#define CONF_TRMLEDBUFFER4ADC_POS       (16)


//typedef enum {
//  ADC_STARTUP_DELAY_1US = 0,
//  ADC_STARTUP_DELAY_8US,
//  ADC_STARTUP_DELAY_12US,
//  ADC_STARTUP_DELAY_16US,
//}AdcStartupDelay_t;


typedef enum {
  ADC_CH4_SEL_GPIO1 = 0,
  ADC_CH4_SEL_GPIO2,
  ADC_CH4_SEL_GPIO3,
  ADC_CH4_SEL_GPIO4,
}AdcCh4Select_t;


typedef enum {
  ADC_CH3_SEL_TEMP_SENSOR = 0,
  ADC_CH3_SEL_VBAT,
}AdcCh3Select_t;

typedef enum {
  ADC_CH2_SEL_CATHODE_LED0 = 0,
  ADC_CH2_SEL_CATHODE_LED1,
  ADC_CH2_SEL_CATHODE_LED2,
}AdcCh2Select_t;


typedef enum {
  ADC_CH0 = 0x00,
  ADC_CH1 = 0x01,
  ADC_CH2 = 0x02,
  ADC_CH3 = 0x04,
  ADC_CH4 = 0x08,
  ADC_CH5 = 0x10,
  ADC_CH1_THEN_CH2 = 0x21,
  ADC_CH1_THEN_CH3 = 0x29,
  ADC_CH2_THEN_CH3 = 0x22,
  ADC_CH2_THEN_CH1 = 0x2A,
  ADC_CH3_THEN_CH1 = 0x24,
  ADC_CH3_THEN_CH2 = 0x2C,
  ADC_CH1_THEN_CH2_THEN_CH3 = 0x31,
  ADC_CH1_THEN_CH3_THEN_CH2 = 0x39,
  ADC_CH2_THEN_CH3_THEN_CH1 = 0x32,
  ADC_CH2_THEN_CH1_THEN_CH3 = 0x3A,
  ADC_CH3_THEN_CH1_THEN_CH2 = 0x34,
  ADC_CH3_THEN_CH2_THEN_CH1 = 0x3C,
}AdcChannelSequence_t;

#define CNTRL_CONVERT_POS       (0)
#define CNTRL_CONT_POS          (1)
#define CNTRL_SYNCENA_POS       (2)
#define CNTRL_STUPDLY_POS       (4)
#define CNTRL_IRQENA_POS        (6)
#define CNTRL_IRQCLR_POS        (7)
#define CNTRL_CHSEQ_POS         (8)
#define CNTRL_CH2SEL_POS        (16)
#define CNTRL_CH4SEL_POS        (20)


//typedef enum {
//  ADC_WAIT_TIME_250NS = 0x00,
//  ADC_WAIT_TIME_500NS,
//  ADC_WAIT_TIME_750NS,
//  ADC_WAIT_TIME_1000NS,
//  ADC_WAIT_TIME_1250NS,
//  ADC_WAIT_TIME_1500NS,
//  ADC_WAIT_TIME_1750NS,
//  ADC_WAIT_TIME_2000NS,
//  ADC_WAIT_TIME_2250NS,
//  ADC_WAIT_TIME_2500NS,
//  ADC_WAIT_TIME_2750NS,
//  ADC_WAIT_TIME_3000NS,
//  ADC_WAIT_TIME_3250NS,
//}AdcWaittingTime_t;

//#define TSET_TCURR_POS       (0)
//#define TSET_TCHNL_POS       (4)
//#define TSET_TGUARD_POS      (8)

typedef void (*adc_cb_func_t)(AdcMeasureItem_t item, uint16_t value);

void ADC_Init(AdcMeasureItem_t item, bool syncEnable);
void ADC_UnInit(AdcMeasureItem_t item);
void ADC_RegisterIRQ(adc_cb_func_t callback);
void ADC_UnregisterIRQ(void);
void ADC_Start(void);



#endif /* __ADC_DEVICE_H__ */